package com.bsj.pay.controller.partner;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bsj.pay.model.ChargeAccountBean;
import com.bsj.pay.model.OrderBean;
import com.bsj.pay.model.PartnerBean;
import com.bsj.pay.service.ChargeAccountService;
import com.bsj.pay.service.OrderService;
import com.bsj.pay.service.PartnerService;
import com.bsj.pay.util.date.DateTimeUtil;

@RequestMapping("/partner")
@Controller("partnerIndexController")
public class IndexController {
	
	@Autowired
	private PartnerService partnerService;
	
	@Autowired
	private ChargeAccountService chargeAccountService;
	
	@Autowired
	private OrderService orderService;
	
	@RequestMapping("")
    public ModelAndView index(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		mav.setViewName("partner/index");
		return mav;
    }
	
	@RequestMapping("/index.html")
    public ModelAndView main(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		mav.setViewName("partner/index");
		return mav;
    }
	
	@RequestMapping("/welcome.html")
    public ModelAndView welcome(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		PartnerBean puser=(PartnerBean) request.getSession().getAttribute("puser");
		OrderBean queryModel=new OrderBean();
		queryModel.setPartner_id(puser.getId());
		queryModel.setStatus(1);
		mav.addObject("allOrderSum", new BigDecimal(orderService.sumMoney(queryModel)).toString());
		
		queryModel.setUpdate_time(DateTimeUtil.getTodayUnixTime());
		//mav.addObject("todayOrderCount", chargeAccountService.count(queryModel));
		
		mav.addObject("todayOrderSum", new BigDecimal(orderService.sumMoney(queryModel)).toString());
		queryModel.setGoods_type(1);;
		mav.addObject("todayOrderSumZz", new BigDecimal(orderService.sumMoney(queryModel)).toString());
		queryModel.setGoods_type(2);
		mav.addObject("todayOrderSumDp", new BigDecimal(orderService.sumMoney(queryModel)).toString());
		queryModel.setGoods_type(3);
		mav.addObject("todayOrderSumQsy", new BigDecimal(orderService.sumMoney(queryModel)).toString());
		
		queryModel = new OrderBean();
		queryModel.setStatus(1);
		queryModel.setPartner_id(puser.getId());
		queryModel.setGoods_type(1);
        List<OrderBean> zzOrders = this.orderService.queryDayList(queryModel);
        queryModel.setGoods_type(2);
        List<OrderBean> dpOrders = this.orderService.queryDayList(queryModel);
        queryModel.setGoods_type(3);
        List<OrderBean> qsyOrders = this.orderService.queryDayList(queryModel);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar calendar = new GregorianCalendar();
        String zzStr = "";
        String dpStr = "";
        String qsyStr = "";
        String dateStr = "";
        for (Integer i = -6; i < 1; ++i) {
            calendar.setTime(date);
            calendar.add(5, i);
            String cDate = df.format(calendar.getTime());
            Long zzSum = 0L;
            Long dpSum = 0L;
            Long qsySum = 0L;
            for (OrderBean order : zzOrders) {
                if (cDate.equals(order.getTime())) {
                	zzSum = order.getAll_amount();
                    break;
                }
            }
            for (OrderBean order : dpOrders) {
                if (cDate.equals(order.getTime())) {
                	dpSum = order.getAll_amount();
                    break;
                }
            }
            for (OrderBean order : qsyOrders) {
                if (cDate.equals(order.getTime())) {
                	qsySum = order.getAll_amount();
                    break;
                }
            }
            zzStr = zzStr + "," + zzSum;
            dpStr = dpStr + "," + dpSum;
            qsyStr = qsyStr + "," + qsySum;
            dateStr = dateStr + "," + cDate;
        }
        mav.addObject("zzStr", zzStr.substring(1));
        mav.addObject("dpStr", dpStr.substring(1));
        mav.addObject("qsyStr", qsyStr.substring(1));
        mav.addObject("dateStr", dateStr.substring(1));
        mav.addObject("quota", partnerService.getById(puser.getId()).getQuota());
		mav.addObject("currentDateTime", DateTimeUtil.getCurrentDateTimeStr());
		mav.setViewName("partner/welcome");
		return mav;
    }
	

	@RequestMapping("/loginPage.html")
    public ModelAndView loginPage(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		mav.setViewName("partner/login");
		return mav;
    }
	
	@RequestMapping("/docPage.html")
    public ModelAndView docPage(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		mav.setViewName("partner/doc");
		return mav;
    }
	
	
	@RequestMapping("/updatePasswordPage.html")
    public ModelAndView updatePasswordPage(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		mav.setViewName("partner/password");
		return mav;
    }
	
	@RequestMapping("/loginOut.html")
    public ModelAndView loginOut(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		request.getSession().removeAttribute("puser");
		mav.setViewName("partner/login");
		return mav;
    }
	
}
